package main

import (
	"fmt"
	"math"
)

func main() {
	a := "1"
	b := "111"

	fmt.Println(addBinary(a, b))
}

func addBinary(a string, b string) string {
	zero := []byte("0")[0]
	one := []byte("1")[0]

	abytes := []byte(a)
	bbytes := []byte(b)
	delt := len(abytes) - len(bbytes)
	deltbytes := make([]byte, int(math.Abs(float64(delt))))
	l := int(math.Max(float64(len(abytes)), float64(len(bbytes))))
	if math.Abs(float64(delt)) > 0 {
		for i := 0; i < int(math.Abs(float64(delt))); i++ {
			deltbytes[i] = []byte("0")[0]
		}

		if delt > 0 {
			bbytes = append(deltbytes, bbytes...)
		} else {
			abytes = append(deltbytes, abytes...)
		}
	}

	rev := make([]byte, l)
	var j byte
	for i := l - 1; i >= 0; i-- {
		if abytes[i] == zero && bbytes[i] == zero {
			if j == one {
				rev[i] = one
				j = zero
			} else {
				rev[i] = zero
			}
		} else if abytes[i] == zero && bbytes[i] == one {
			if j == one {
				rev[i] = zero
			} else {
				rev[i] = one
			}
		} else if abytes[i] == one && bbytes[i] == zero {
			if j == one {
				rev[i] = zero
			} else {
				rev[i] = one
			}
		} else if abytes[i] == one && bbytes[i] == one {
			if j == one {
				rev[i] = one
			} else {
				rev[i] = zero
				j = one
			}
		}
	}

	if j == one {
		return string([]byte{j}) + string(rev)
	}

	return string(rev)
}
